<?xml version="1.0" encoding="UTF-8"?>
<!-- 
    ***************************************************************************
    Updated: 02/05/2009
    
    TODO: 
    1. Since MonitoredDataItem could not appear if IsContextType="false", we need to add cases to all policy assertions xslt we made so far.
    
    Change Logs:
    1. fixed default value output
    3. fixed typing error about ture/false (from T/F)
    2. add namespaces definition in every MD output part
    3. normalize spaces (removing leading or trailing spaces) of ontologicalMeaning
    4. fixed output if there's only 1 parameter for eventDefinition
    5. sequence number added
    6. add copying all content in configurationData in config file to output
    7. add a temporary root element in output xml for well-formed.
    8. namespaces finished
    9. problem that namespace URI would appear inside element, like (<masc-ex:boolean="...." xmlns:masc-ex=".........">), is fixed
    10. Boolean Expression is finished and tested for different cases (please check if the example in each cases I inclued in comment is in right structure)
    11. add optional schedule parts
    
    
    Additional Info:
    1. there's no output for PassedInSOAPHeaders for example. since there's no corresponding MD-Additioanl Info in config file
    2. I found that for character like '<', '>', '"', etc, there're 2 sets of entity references (e.g. for '<', could be '&lt;' or '&#60;')
        check here for further info (http://en.wikipedia.org/wiki/Character_entity_reference)
        I think we shoud use xml standard which are: &lt;, &gt;, &amp;, &quot;, or &apos;, 
        but just for the example, we use &#x201d; for getting right result, in the future we can replace all &#201d; with &quod; and make a note for that
    3. WhenFor is not defined yet, since it's not a big issue, we will consider that later.
    
    ****************************************************************************
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xmi="http://schema.omg.org/spec/XMI/2.1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:MASC_Profile="http:///schemas/MASC_Profile/_OnYS4BTgEd6yO-ZURVEtWw/6"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:uml="http://www.eclipse.org/uml2/2.1.0/UML"
    xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy"
    xmlns:masc-gp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp"
    xmlns:masc-se="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se"
    xmlns:masc-ex="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex"
    xmlns:masc-om="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om"
    xmlns:masc-sc="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc"
    xmlns:masc-ap="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap"
    xmlns:masc-mp="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp"
    xmlns:masc-up="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up"
    xmlns:masc-config="http://www.nicta.com.au/projects/armature/masc-config"
    xmlns:wsa="http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa"
    xmlns:QoSOntology="http://www.myontologyrepository.com/qosontology.owl"
    xmlns:tns="${currentFileURL}" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs" version="2.0">

    <xsl:output method="xml" indent="yes"/>

    <xsl:variable name="input" select="document('../inputs/StockTradingGPA.xml')"/>
    <xsl:variable name="config" select="document('../inputs/config.xml')"/>

    <!-- define a newline variable for pretty output -->
    <xsl:variable name="newline">
        <xsl:text>&#10;</xsl:text>
    </xsl:variable>
    <!-- define the target namespace -->
    <xsl:variable name="tns">
        <xsl:text>tns:</xsl:text>
    </xsl:variable>

    <xsl:template match="/" name="MD_Trans">
        <xsl:element name="root">
            <xsl:comment>
                <xsl:text>This root element is for well-formed of output. Could be removed if we generate a corresponding output file with every Policy Assertion/Monitored Definition</xsl:text>
            </xsl:comment>
            <xsl:value-of select="$newline"/>
            <xsl:value-of select="$newline"/>
            <xsl:apply-templates select="$input/xmi:XMI"/>
        </xsl:element>
        <xsl:value-of select="$newline"/>
    </xsl:template>


    <xsl:template match="xmi:XMI">
        <xsl:for-each select="$input/xmi:XMI/MASC_Profile:MonitoringDef">
            <xsl:variable name="MD_MASCID" select="./@MD-MASCID"/>
            <xsl:variable name="valueDataType" select="./@ValueDataType"/>
            <xsl:variable name="unit" select="./@Unit"/>
            <xsl:variable name="ontologicalMeaning" select="./@OntologicalMeaning"/>
            <xsl:variable name="md-managementParty" select="./@MD-ManagementParty"/>
            <xsl:variable name="md-When-TriggerEvents" select="./@MD-When-TriggerEvents"/>
            <xsl:variable name="baseElement" select="./@base_Element"/>
            <xsl:variable name="monitoredFor" select="./@monitoredFor"/>
            <xsl:variable name="SN">
                <!-- define sequence number by MD's position -->
                <xsl:number value="position()" format="1"/>
            </xsl:variable>

            <xsl:for-each select="$input//packagedElement/node">
                <xsl:variable name="name" select="@name"/>
                <xsl:variable name="xmiID" select="@xmi:id"/>

                <xsl:choose>
                    <xsl:when test="$xmiID=$baseElement">

                        <!--
                            ***********************************************************

                                    varibales for monitoring definition constructs

                            ***********************************************************
                        -->
                        <!-- variables for ontological meaning -->
                        <xsl:variable name="MD_ON">
                            <!-- ontology name -->
                            <xsl:value-of
                                select="normalize-space(substring-before($ontologicalMeaning, ':'))"
                            />
                        </xsl:variable>
                        <xsl:variable name="MD_OC">
                            <!-- ontology construct -->
                            <xsl:value-of
                                select="normalize-space(substring-after($ontologicalMeaning, ':'))"
                            />
                        </xsl:variable>
                        <xsl:variable name="MD_OL">
                            <!-- ontology language -->
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/@OntologicalLanguage"
                            />
                        </xsl:variable>

                        <!-- variables for monitored data items -->
                        <xsl:variable name="MD_MPN">
                            <!-- messagePartName -->
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/@MessagePartName"
                            />
                        </xsl:variable>
                        <xsl:variable name="MD_OMMASCID">
                            <xsl:text>OM-</xsl:text>
                            <xsl:value-of select="$MD_ON"/>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="$MD_OC"/>
                        </xsl:variable>

                        <!-- variables for states -->
                        <xsl:variable name="MD_SD">
                            <!-- state definition -->
                            <xsl:value-of
                                select="substring-before(substring-after($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/@When-AllowedStates, '{'), '}')"
                            />
                        </xsl:variable>

                        <!-- variables for events -->
                        <xsl:variable name="MD_temp1">
                            <xsl:value-of select="$md-When-TriggerEvents"/>
                        </xsl:variable>
                        <xsl:variable name="MD_ET">
                            <!-- event type -->
                            <xsl:value-of
                                select="substring-after(substring-before($MD_temp1, '('), '{')"/>
                        </xsl:variable>

                        <xsl:variable name="MD_temp2">
                            <xsl:value-of
                                select="substring-after(substring-before($MD_temp1, ')'), '(')"/>
                        </xsl:variable>
                        <xsl:variable name="MD_ParamList" select="tokenize($MD_temp2,',')"/>
                        <xsl:variable name="MD_P1">
                            <xsl:value-of select="normalize-space($MD_ParamList[position()=1])"/>
                        </xsl:variable>
                        <xsl:variable name="MD_P2">
                            <xsl:value-of select="normalize-space($MD_ParamList[position()=2])"/>
                        </xsl:variable>
                        <xsl:variable name="MD_P3">
                            <xsl:value-of select="normalize-space($MD_ParamList[position()=3])"/>
                        </xsl:variable>
                        <xsl:variable name="MD_P4">
                            <xsl:value-of select="normalize-space($MD_ParamList[position()=4])"/>
                        </xsl:variable>
                        <xsl:variable name="MD_P5">
                            <xsl:value-of select="normalize-space($MD_ParamList[position()=5])"/>
                        </xsl:variable>
                        <xsl:variable name="MD_P6">
                            <xsl:value-of select="normalize-space($MD_ParamList[position()=6])"/>
                        </xsl:variable>

                        <xsl:variable name="PN">
                            <!-- process name -->
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/@UMLDiagramName"
                            />
                        </xsl:variable>
                        <xsl:variable name="AN">
                            <!-- activity name -->
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/@UMLElementName"
                            />
                        </xsl:variable>

                        <!-- variables for when constructs -->
                        <xsl:variable name="MD_WF">
                            <!-- When For -->
                            <!-- not finished -->
                            <xsl:text>WF</xsl:text>
                        </xsl:variable>
                        <xsl:variable name="MD_WFMASCID">
                            <!-- value of WhenFor MASCID -->
                            <!-- not finished -->
                            <!-- user MD_MASCID-MD_ET for now -->
                            <xsl:value-of select="$MD_MASCID"/>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="$MD_ET"/>
                        </xsl:variable>
                        <xsl:variable name="MD_ED">
                            <!-- mascid of previously defined Event Definition Construct -->
                            <xsl:value-of select="$MD_ET"/>
                            <xsl:text>-</xsl:text>
                            <xsl:value-of select="$MD_P1"/>
                            <xsl:if test="normalize-space($MD_P2)!=''">
                                <xsl:text>-</xsl:text>
                                <xsl:value-of select="$MD_P2"/>
                            </xsl:if>
                        </xsl:variable>

                        <!-- variables for boolean expressions for monitoring definition are in their templates based on different cases -->

                        <!-- variables for monitoring data collection -->
                        <xsl:variable name="MD_MO">
                            <!-- prefix for monitoring data collection MASCID -->
                            <xsl:text>MonitoringOf</xsl:text>
                        </xsl:variable>
                        <xsl:variable name="MD_PassedSOAPHeaders">
                            <!-- passed soap headers -->
                            <xsl:value-of
                                select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/@PassedInSOAPHeaders"
                            />
                        </xsl:variable>

                        <!-- variables for action policy assertions configuring monitoring -->
                        <xsl:variable name="MD_APAM">
                            <!-- prefix for MASCID -->
                            <xsl:text>APAMonitor</xsl:text>
                        </xsl:variable>

                        <!-- variables for NameSpaces of MD-->
                        <xsl:variable name="MD_TNS">
                            <xsl:value-of select="$PN"/>
                            <xsl:text>-MD-WSP4MASC</xsl:text>
                        </xsl:variable>

                        <!-- the output templte has to be here, cannot use attributes set -->
                        <!--
                            ***********************************************************
                            
                            Define output for MD
                            
                            ***********************************************************
                        -->
                        <xsl:comment>
                            <xsl:text>OUTPUT FOR MONITORING DEFINITION for UML Element: </xsl:text>
                            <xsl:value-of select="$name"/>
                            <xsl:value-of select="$newline"/>
                            <xsl:text> The MASCID is: </xsl:text>
                            <xsl:value-of select="$MD_MASCID"/>
                        </xsl:comment>
                        <xsl:value-of select="$newline"/>
                        <xsl:element name="wsp:Policy">
                            <!-- constant namespaces -->
                            <xsl:namespace name="xs" select="'http://www.w3.org/2001/XMLSchema'"/>
                            <xsl:namespace name="wsp"
                                select="'http://schemas.xmlsoap.org/ws/2004/09/policy'"/>
                            <xsl:namespace name="masc-gp"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp'"/>
                            <xsl:namespace name="masc-se"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se'"/>
                            <xsl:namespace name="masc-ex"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex'"/>
                            <xsl:namespace name="masc-om"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om'"/>
                            <xsl:namespace name="masc-sc"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc'"/>
                            <xsl:namespace name="masc-ap"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap'"/>
                            <xsl:namespace name="masc-mp"
                                select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp'"/>
                            <xsl:namespace name="masc-up"
                            select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up'"/>
                            <xsl:namespace name="masc-config" select="'http://www.nicta.com.au/projects/armature/masc-config'"/>
                            <xsl:namespace name="wsa" select="'http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/wsa'"/>
                            <!-- schema locations -->
                            <xsl:namespace name="xsi"
                                select="'http://www.w3.org/2001/XMLSchema-instance'"/>
                            <xsl:namespace name="schemaLocation"
                                select="'http://schemas.xmlsoap.org/ws/2004/09/policy 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-gp 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-gp.xsd
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-se 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-se.xsd
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ex 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ex.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-om 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-sc.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-sc 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-om.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-ap 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-ap.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-up 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-up.xsd 
                                http://www.sce.unsw.edu.au/~vtosic/ws-policy4masc/masc-mp 
                                C:\Users\Gong\Volunteer Project\GPA_Transform\Schemas\ws-policy4masc-mp.xsd'"/>
                            <!-- namespaces for TargetNameSpace -->
                            <xsl:namespace name="targetNamespace" select="$MD_TNS"/>
                            <xsl:namespace name="tns" select="$MD_TNS"/>
                            <!-- namespaces for QoSOntology, MeasurementOntology and WSDLs -->
                            <xsl:namespace name="QoSOntology"
                                select="$config/masc-config:XMIFile/namespace::*[normalize-space(lower-case(local-name()))='qosontology']"/>
                            <xsl:namespace name="MeasurementOntology"
                                select="$config/masc-config:XMIFile/namespace::*[normalize-space(lower-case(local-name()))='measurementontology']"/>
                            <xsl:for-each
                                select="$config/masc-config:XMIFile/namespace::*[starts-with(normalize-space(lower-case(local-name())), 'wsdl')]">
                                <xsl:namespace name="{name()}">
                                    <xsl:value-of select="."/>
                                </xsl:namespace>
                            </xsl:for-each>
                            <xsl:value-of select="$newline"/>

                            <xsl:comment>
                                <xsl:text>Definition of OntologicalMeaning</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-om:OntologicalMeaning">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="OM-MD_ON-MD_OC" -->
                                    <xsl:text>OM-</xsl:text>
                                    <xsl:value-of select="$MD_ON"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$MD_OC"/>
                                </xsl:attribute>
                                <xsl:attribute name="OntologicalDefinition">
                                    <!-- OntologicalDefinition="MD_ON:MD_OC" -->
                                    <xsl:value-of select="$MD_ON"/>
                                    <xsl:text>:</xsl:text>
                                    <xsl:value-of select="$MD_OC"/>
                                </xsl:attribute>
                                <xsl:attribute name="OntologicalLanguage">
                                    <!-- OntologicalLanguage="MD_OL" -->
                                    <xsl:value-of select="$MD_OL"/>
                                </xsl:attribute>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <xsl:comment>
                                <xsl:text>Definition of Monitored Data Items</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-gp:MonitoredDataItem">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="MD_MASCID" -->
                                    <xsl:value-of select="$MD_MASCID"/>
                                </xsl:attribute>
                                <xsl:attribute name="ValueDataType">
                                    <!-- ValueDataType="valueDataType" -->
                                    <xsl:value-of select="$valueDataType"/>
                                </xsl:attribute>
                                <xsl:attribute name="Unit">
                                    <!-- Unit="unit" -->
                                    <xsl:value-of select="$unit"/>
                                </xsl:attribute>
                                <xsl:attribute name="MessagePartName">
                                    <!-- MessagePartName="MD_MPN" -->
                                    <xsl:value-of select="$MD_MPN"/>
                                </xsl:attribute>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-om:OntologicalMeaningRef">
                                <xsl:attribute name="To">
                                    <!-- To="tns:OM-MD_ON-MD_OC" -->
                                    <xsl:value-of select="$tns"/>
                                    <xsl:text>OM-</xsl:text>
                                    <xsl:value-of select="$MD_ON"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$MD_OC"/>
                                </xsl:attribute>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <xsl:comment>
                                <xsl:text>Definition of States</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-se:StateDefinition">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="MD_SD" -->
                                    <xsl:value-of select="$MD_SD"/>
                                </xsl:attribute>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <!-- event constructs -->
                            <xsl:comment>
                                <xsl:text>Definition of Events</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-se:EventDefinition">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="MD_ET-MD_P1-MD_P2" up to 2 parameters would be shown-->
                                    <xsl:value-of select="$MD_ED"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$SN"/>
                                </xsl:attribute>
                                <!-- cases of events -->
                                <xsl:choose>
                                    <!-- case1: ActivityEnded or ActivityStarted -->
                                    <xsl:when test="contains(lower-case($MD_ET), 'activityended')">
                                        <xsl:element name="masc-se:{$MD_ET}">
                                            <xsl:attribute name="ActivityName">
                                                <xsl:value-of select="$AN"/>
                                            </xsl:attribute>
                                            <xsl:attribute name="Successful">
                                                <xsl:value-of select="$MD_P2"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <xsl:when test="contains(lower-case($MD_ET), 'activitystarted')">
                                        <xsl:element name="masc-se:{$MD_ET}">
                                            <xsl:attribute name="ActivityName">
                                                <xsl:value-of select="$AN"/>
                                            </xsl:attribute>
                                            <xsl:attribute name="Successful">
                                                <xsl:value-of select="$MD_P2"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case2: MonitoredDataItemUpdated -->
                                    <xsl:when
                                        test="contains(lower-case($MD_ET), 'monitoreddataitemupdated')">
                                        <xsl:element name="masc-se:MonitoredDataItemUpdated">
                                            <xsl:attribute name="ItemID">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case3: GoalStatisfaction -->
                                    <xsl:when
                                        test="contains(lower-case($MD_ET), 'goalsatisfaction')">
                                        <xsl:element name="masc-se:GoalStatisfaction">
                                            <xsl:attribute name="GoalPolicyAssertionID">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                            <xsl:attribute name="Satisfied">
                                                <xsl:value-of select="$MD_P2"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case4: OperationInvocation -->
                                    <xsl:when
                                        test="contains(lower-case($MD_ET), 'operationinvocation')">
                                        <xsl:element name="masc-se:OperationInvocation">
                                            <xsl:element name="masc-sc:ScopeOperation">
                                                <xsl:element name="masc-sc:ServiceName">
                                                  <xsl:attribute name="Name">
                                                  <xsl:value-of select="$MD_P1"/>
                                                  <xsl:text>:</xsl:text>
                                                  <xsl:value-of select="$MD_P2"/>
                                                  </xsl:attribute>
                                                  <xsl:attribute name="Completed">
                                                  <xsl:value-of select="$MD_P5"/>
                                                  </xsl:attribute>
                                                  <xsl:attribute name="FaultsOccured">
                                                  <xsl:value-of select="$MD_P6"/>
                                                  </xsl:attribute>
                                                </xsl:element>
                                                <xsl:element name="masc-sc:EndpointName">
                                                  <xsl:attribute name="Name">
                                                  <xsl:value-of select="$MD_P1"/>
                                                  <xsl:text>:</xsl:text>
                                                  <xsl:value-of select="$MD_P3"/>
                                                  </xsl:attribute>
                                                </xsl:element>
                                                <xsl:element name="masc-sc:OperationName">
                                                  <xsl:attribute name="Name">
                                                  <xsl:value-of select="$MD_P1"/>
                                                  <xsl:text>:</xsl:text>
                                                  <xsl:value-of select="$MD_P4"/>
                                                  </xsl:attribute>
                                                </xsl:element>
                                            </xsl:element>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case5: MessageReceipt, MessageToBeSent and MessageSending -->
                                    <xsl:when test="starts-with(lower-case($MD_ET), 'message')">
                                        <xsl:element name="masc-se:{$MD_ET}">
                                            <xsl:element name="masc-sc:ScopeMessage">
                                                <xsl:element name="masc-sc:ServiceName">
                                                  <xsl:attribute name="Name">
                                                  <xsl:value-of select="$MD_P1"/>
                                                  <xsl:text>:</xsl:text>
                                                  <xsl:value-of select="$MD_P2"/>
                                                  </xsl:attribute>
                                                  <xsl:attribute name="Completed">
                                                  <xsl:value-of select="$MD_P5"/>
                                                  </xsl:attribute>
                                                </xsl:element>
                                                <xsl:element name="masc-sc:EndpointName">
                                                  <xsl:attribute name="Name">
                                                  <xsl:value-of select="$MD_P1"/>
                                                  <xsl:text>:</xsl:text>
                                                  <xsl:value-of select="$MD_P3"/>
                                                  </xsl:attribute>
                                                </xsl:element>
                                                <xsl:element name="masc-sc:OperationName">
                                                  <xsl:attribute name="Name">
                                                  <xsl:value-of select="$MD_P1"/>
                                                  <xsl:text>:</xsl:text>
                                                  <xsl:value-of select="$MD_P4"/>
                                                  </xsl:attribute>
                                                </xsl:element>
                                                <xsl:element name="masc-sc:MessageName">
                                                  <xsl:attribute name="Name">
                                                  <xsl:value-of select="$MD_P5"/>
                                                  </xsl:attribute>
                                                </xsl:element>
                                            </xsl:element>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case6: ActionCompletion -->
                                    <xsl:when
                                        test="contains(lower-case($MD_ET), 'actioncompletion')">
                                        <xsl:element name="masc-se:ActionCompletion">
                                            <xsl:attribute name="ActionPolicyAssertionID">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                            <xsl:attribute name="Completed">
                                                <xsl:value-of select="$MD_P2"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case7: ScheduledEventOccurence -->
                                    <xsl:when
                                        test="contains(lower-case($MD_ET), 'scheduledeventoccurence')">
                                        <xsl:element name="masc-se:ScheduledEventOccurence">
                                            <xsl:attribute name="scheduleID">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case8: ProcessInstanceStart and ProcessInstanceFinished -->
                                    <xsl:when
                                        test="starts-with(lower-case($MD_ET), 'processinstance')">
                                        <xsl:element name="masc-se:PIT">
                                            <xsl:attribute name="ProcessType">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                            <xsl:attribute name="Completed">
                                                <xsl:value-of select="$MD_P2"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case9: PartyToBeInitialized and PartyToBeFinalized -->
                                    <xsl:when test="starts-with($MD_ET, 'partytobe')">
                                        <xsl:element name="masc-se:PIF">
                                            <xsl:attribute name="PartyName">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case10: StateEntrance, StateTransitioning and StateExit -->
                                    <xsl:when
                                        test="(contains(lower-case($MD_ET), 'stateentrance')) or (contains(lower-case($MD_ET),'statetransitioning')) or (contains(lower-case($MD_ET), 'stateexit'))">
                                        <xsl:element name="masc-se:STTE">
                                            <xsl:attribute name="To">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case11: PolicyAssertionAddition and PolicyAssertionDeletion -->
                                    <xsl:when
                                        test="starts-with(lower-case($MD_ET), 'policyassertion')">
                                        <xsl:element name="masc-se:PT">
                                            <xsl:attribute name="PolicyAssertionID">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                    <!-- case12: MonitoredContextPropertyChanged -->
                                    <xsl:when
                                        test="contains(lower-case($MD_ET), 'monitoredcontextpropertychanged')">
                                        <xsl:element name="masc-se:MonitoredContextPropertyChanged">
                                            <xsl:attribute name="ContextPropertyID">
                                                <xsl:value-of select="$tns"/>
                                                <xsl:value-of select="$MD_P1"/>
                                            </xsl:attribute>
                                        </xsl:element>
                                    </xsl:when>
                                </xsl:choose>
                                <!-- cases end -->
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <xsl:comment>
                                <xsl:text>Definition of When Constructs</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-se:When">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="MD_WF-MD_WFMASCID" -->
                                    <xsl:value-of select="$MD_WF"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$MD_WFMASCID"/>
                                </xsl:attribute>
                                <xsl:element name="masc-se:AllowedStates">
                                    <xsl:element name="masc-se:StateRef">
                                        <xsl:attribute name="To">
                                            <!-- StateRef To="tns:MD_SD" -->
                                            <xsl:value-of select="$tns"/>
                                            <xsl:value-of select="$MD_SD"/>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:element>
                                <xsl:element name="masc-se:PossibleTriggerEvents">
                                    <xsl:element name="masc-se:EventRef">
                                        <xsl:attribute name="To">
                                            <!-- EventRef To="tns:MD_ED" -->
                                            <xsl:value-of select="$tns"/>
                                            <xsl:value-of select="$MD_ED"/>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:element>
                                <xsl:element name="masc-se:BooleanExpression">
                                    <xsl:element name="masc-se:BooleanExpressionRef">
                                        <xsl:attribute name="To">
                                            <xsl:value-of select="$tns"/>
                                            <!-- BooleanExp MASCID varies for cases -->
                                            <!-- TODO: should be more easier after tns done -->
                                            <xsl:choose>
                                                <!-- case1. Boolean Constants  -->
                                                <xsl:when
                                                  test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition[./True or ./False]">
                                                  <xsl:text>MD-BC-</xsl:text>
                                                  <xsl:value-of select="$MD_MASCID"/>
                                                </xsl:when>
                                                <!-- case2 just 1 comparisions -->
                                                <xsl:when
                                                  test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition[./Equal or ./NotEqual or ./Less or ./LessOrEqual or ./Greater or ./GreaterOrEqual]">
                                                  <xsl:text>MD-BE-</xsl:text>
                                                  <xsl:value-of select="$MD_MASCID"/>
                                                  <xsl:text>-</xsl:text>
                                                  <xsl:value-of
                                                  select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition//MonitoredData/@Name"/>
                                                  <xsl:text>-</xsl:text>
                                                  <xsl:value-of
                                                  select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition//MonitoredData), '([a-z]|[A-Z])', '')"
                                                  />
                                                </xsl:when>
                                                <!-- case3 Comparator with 2 comparisions -->
                                                <xsl:when
                                                  test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition[./AND or ./OR]">
                                                  <xsl:text>MD-BE-</xsl:text>
                                                  <xsl:value-of
                                                  select="node-name($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition/*)"/>
                                                  <xsl:text>-</xsl:text>
                                                  <!-- generate id for ./OT (e.g. WhenFilteringCondition/AND) as sequence number -->
                                                  <xsl:value-of
                                                  select="replace(generate-id($config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition/*), '([a-z]|[A-Z])', '')"
                                                  />
                                                </xsl:when>
                                            </xsl:choose>
                                        </xsl:attribute>
                                    </xsl:element>
                                </xsl:element>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <!-- boolean expressions constructs -->
                            <xsl:comment>
                                <xsl:text>Definition of optional BooleanExpressions for monitoring definition</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <!-- could add a for loop for multiple When-FilteringCondition -->
                            <xsl:choose>
                                <!-- if can not find MD Additional Info for selected stereotype -->
                                <xsl:when
                                    test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')][not(masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID])]">
                                    <xsl:text>Can not find corresponding MD Additional Info in config file</xsl:text>
                                </xsl:when>
                                <xsl:otherwise>
                                    <xsl:apply-templates
                                        select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/When-FilteringCondition"
                                    />
                                </xsl:otherwise>
                            </xsl:choose>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>
                            
                            <!-- Optional Schedule Definition -->
                            <!-- if has optional schedules definition -->
                            <xsl:if test="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-se:Schedules">
                                <xsl:comment>
                                    <xsl:text>Definition of Schedule of </xsl:text>
                                    <xsl:value-of select="$name"/>
                                </xsl:comment>
                                <xsl:value-of select="$newline"/>
                                <xsl:copy-of select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-se:Schedules"/>
                                <xsl:value-of select="$newline"/>
                                <xsl:value-of select="$newline"/>
                            </xsl:if>


                            <!-- Monitoring Data Collection -->
                            <xsl:comment>
                                <xsl:text>Definition of Monitoring Data Collection</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-ap:MonitoredDataCollection">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="MD_MO-MD_MASCID" -->
                                    <xsl:value-of select="$MD_MO"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$MD_MASCID"/>
                                </xsl:attribute>
                                <xsl:attribute name="PassedInSOAPHeaders">
                                    <!-- MD_PassedSOAPHeaders="T/F" -->
                                    <xsl:choose>
                                        <xsl:when
                                            test="contains(lower-case($MD_PassedSOAPHeaders),'true')">
                                            <xsl:text>true</xsl:text>
                                        </xsl:when>
                                        <xsl:when
                                            test="contains(lower-case($MD_PassedSOAPHeaders),'false')">
                                            <xsl:text>false</xsl:text>
                                        </xsl:when>
                                    </xsl:choose>
                                </xsl:attribute>
                                <xsl:element name="masc-gp:MonitoredDataItemRef">
                                    <xsl:attribute name="To">
                                        <!-- MonitoredDataItemRef To="tns:MD_MASCID" -->
                                        <xsl:value-of select="$tns"/>
                                        <xsl:value-of select="$MD_MASCID"/>
                                    </xsl:attribute>
                                    <xsl:element name="masc-ap:ConfigurationData">
                                        <!-- copy all context in configurationData -->
                                        <xsl:copy-of
                                            select="$config/masc-config:XMIFile/masc-config:UMLDiagram/masc-config:UMLElement[replace(lower-case(@UMLElementName),'\s+','')=replace(lower-case($name),'\s+','')]/masc-config:MonitoringDef-AdditionalInfo[@MASCID=$MD_MASCID]/ConfigurationData/*"
                                        />
                                    </xsl:element>
                                </xsl:element>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <!-- Monitoring Action Groups -->
                            <xsl:comment>
                                <xsl:text>Definition of Monitoring Action Groups</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-ap:ActionGroup">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="AG-MD_MO-MD_MASCID" -->
                                    <xsl:text>AG-</xsl:text>
                                    <xsl:value-of select="$MD_MO"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$MD_MASCID"/>
                                </xsl:attribute>
                                <xsl:element name="masc-gp:MonitoredDataCollectionRef">
                                    <xsl:attribute name="To">
                                        <!-- MonitoredDataCollectionRef To="tns:MD_MO-MD_MASCID" -->
                                        <xsl:value-of select="$tns"/>
                                        <xsl:value-of select="$MD_MO"/>
                                        <xsl:text>-</xsl:text>
                                        <xsl:value-of select="$MD_MASCID"/>
                                    </xsl:attribute>
                                </xsl:element>
                            </xsl:element>
                            <xsl:value-of select="$newline"/>
                            <xsl:value-of select="$newline"/>

                            <!-- Action Policy Assertions configuring monitoring -->
                            <xsl:comment>
                                <xsl:text>Definition of Action Policy Assertions configuring monitoring</xsl:text>
                            </xsl:comment>
                            <xsl:value-of select="$newline"/>
                            <xsl:element name="masc-ap:ActionPolicyAssertion">
                                <xsl:attribute name="MASCID">
                                    <!-- MASCID="MD_APAM-MD_MASCID" -->
                                    <xsl:value-of select="$MD_APAM"/>
                                    <xsl:text>-</xsl:text>
                                    <xsl:value-of select="$MD_MASCID"/>
                                </xsl:attribute>
                                <xsl:attribute name="ManagementParty">
                                    <xsl:value-of select="$md-managementParty"/>
                                </xsl:attribute>
                                <xsl:element name="masc-se:WhenRef">
                                    <xsl:attribute name="To">
                                        <xsl:value-of select="$tns"/>
                                        <xsl:value-of select="$MD_WF"/>
                                        <xsl:text>-</xsl:text>
                                        <xsl:value-of select="$MD_MASCID"/>
                                    </xsl:attribute>
                                </xsl:element>
                                <xsl:element name="masc-ap:ActionGroupRef">
                                    <xsl:attribute name="To">
                                        <!-- ActionGroupRef To="tns:AG-MD_MO-MD_MASCID" -->
                                        <xsl:value-of select="$tns"/>
                                        <xsl:text>AG-</xsl:text>
                                        <xsl:value-of select="$MD_MO"/>
                                        <xsl:text>-</xsl:text>
                                        <xsl:value-of select="$MD_MASCID"/>
                                    </xsl:attribute>
                                </xsl:element>
                            </xsl:element>
                        </xsl:element>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>


    <!-- tempates for booleanExpression cases (for When-FilteringCondition) -->
    <xsl:template match="masc-config:MonitoringDef-AdditionalInfo/When-FilteringCondition">
        <xsl:variable name="MD_MASCID" select="../@MASCID"/>
        <!-- variables of Operator Type (e.g. AND, OR, True, False, Equal, NotEqual, etc) -->
        <xsl:variable name="OT" select="node-name(child::*)"/>

        <!-- case1. Boolean Constants
            e.g.
            <When-Filtering-Condition>
                <True/>
            </When-Filtering-Condition> 
        -->

        <xsl:if test="./True or ./False">
            <xsl:element name="masc-ex:BooleanExpression">
                <xsl:attribute name="MASCID">
                    <xsl:text>MD-BC-</xsl:text>
                    <xsl:value-of select="$MD_MASCID"/>
                </xsl:attribute>
                <xsl:element name="masc-ex:BooleanConstant">
                    <xsl:attribute name="Type" select="lower-case(string($OT))"/>
                </xsl:element>
            </xsl:element>
        </xsl:if>

        <!-- case2 just 1 comparisions -->
        <xsl:if
            test="./Equal or ./NotEqual or ./Less or ./LessOrEqual or ./Greater or ./GreaterOrEqual">
            <xsl:choose>
                <!-- case2.1 with Operater in AwUExpression
                    e.g.
                    <Equal>
                    <Add>
                    <MonitoredData Name=""/>
                    <AwUConst Value="" Unit=""/>
                    </Add>
                    </Equal>   
                -->
                <xsl:when test="string(node-name(*/*))='Add' or string(node-name(*/*))='Multiply'">
                    <xsl:variable name="operater" select="node-name(*/*)"/>
                    <xsl:variable name="AwUExpUnit" select="*/*/AwUConst/@Unit"/>
                    <xsl:call-template name="Comparision">
                        <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                        <xsl:with-param name="C" select="$OT"/>
                        <xsl:with-param name="MDIN" select="*/*/MonitoredData/@Name"/>
                        <xsl:with-param name="operater" select="$operater"/>
                        <xsl:with-param name="AwUExpValue" select="*/*/AwUConst/@Value"/>
                        <xsl:with-param name="SN_MonitoredData"
                            select="replace(generate-id(*/*/MonitoredData), '([a-z]|[A-Z])', '')"/>
                        <xsl:with-param name="SN_AwUConst"
                            select="replace(generate-id(*/*/AwUConst), '([a-z]|[A-Z])', '')"/>
                        <xsl:with-param name="OURI" select="substring-before($AwUExpUnit, ':')"/>
                        <xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- case2.2 without Operater inside AwUExpression
                    e.g.
                    <Equal>
                    <MonitoredData Name=""/>
                    <AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>)
                    </Equal>   
                -->
                <xsl:otherwise>
                    <xsl:variable name="AwUExpValue" select="*/AwUConst/@Value"/>
                    <xsl:variable name="AwUExpUnit" select="*/AwUConst/@Unit"/>
                    <xsl:call-template name="Comparision">
                        <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                        <xsl:with-param name="MDIN" select="*/MonitoredData/@Name"/>
                        <xsl:with-param name="C" select="$OT"/>
                        <xsl:with-param name="AwUExpValue" select="$AwUExpValue"/>
                        <xsl:with-param name="AwUExpUnit" select="$AwUExpUnit"/>
                        <xsl:with-param name="OURI" select="substring-before($AwUExpUnit, ':')"/>
                        <xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
                        <!-- generate id for ./OT/C/MonitoredData (e.g. WhenFilteringCondition/Equal/MonitoredData) as sequence number -->
                        <xsl:with-param name="SN_MonitoredData"
                            select="replace(generate-id(*/MonitoredData), '([a-z]|[A-Z])', '')"/>
                        <!-- generate id for ./OT/C/AwUConst (e.g. WhenFilteringCondition/Equal/AwUConst) as sequence number -->
                        <xsl:with-param name="SN_AwUConst"
                            select="replace(generate-id(*/AwUConst), '([a-z]|[A-Z])', '')"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>

        <!-- case3 Comparator with 2 comparisions -->
        <xsl:if test="./AND or ./OR">
            <xsl:element name="masc-ex:BooleanExpression">
                <xsl:attribute name="MASCID">
                    <xsl:text>MD-BE-</xsl:text>
                    <xsl:value-of select="$OT"/>
                    <xsl:text>-</xsl:text>
                    <!-- generate id for ./OT (e.g. WhenFilteringCondition/AND) as sequence number -->
                    <xsl:value-of select="replace(generate-id(child::*), '([a-z]|[A-Z])', '')"/>
                </xsl:attribute>
                <xsl:element name="masc-ex:BinaryBooleanOperator">
                    <xsl:attribute name="Type" select="$OT"/>
                </xsl:element>
                <xsl:for-each select="*/child::*">
                    <xsl:choose>
                        <!-- case3.1 with Operater inside AwUExpression
                            e.g.
                            <When-Filtering-Condition>
                                <AND>
                                    <Equal>
                                        <Add>
                                            <MonitoredData Name=""/>
                                            <AwUConst Value="" Unit=""/>
                                        </Add>
                                    </Equal>
                                    <NotEqual>
                                        <Multiply>
                                            <MonitoredData Name=""/>
                                            <AwUConst Value=""/>
                                        <Multiply>
                                    </NotEqual>
                                </AND>
                            </When-Filtering-Condition>
                        -->
                        <xsl:when test="./Add or ./Multiply">
                            <xsl:variable name="AwUExpValue" select="*/AwUConst/@Value"/>
                            <xsl:variable name="AwUExpUnit" select="*/AwUConst/@Unit"/>
                            <xsl:variable name="operater" select="node-name(*)"/>
                            <xsl:call-template name="Comparision">
                                <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                                <xsl:with-param name="MDIN" select="*/MonitoredData/@Name"/>
                                <xsl:with-param name="C" select="local-name()"/>
                                <xsl:with-param name="operater" select="$operater"/>
                                <xsl:with-param name="AwUExpValue" select="$AwUExpValue"/>
                                <xsl:with-param name="AwUExpUnit" select="$AwUExpUnit"/>
                                <xsl:with-param name="OURI"
                                    select="substring-before($AwUExpUnit, ':')"/>
                                <xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
                                <!-- generate id for ./OT/C/MonitoredData (e.g. WhenFilteringCondition/AND/Equal/MonitoredData) as sequence number -->
                                <xsl:with-param name="SN_MonitoredData"
                                    select="replace(generate-id(*/MonitoredData), '([a-z]|[A-Z])', '')"/>
                                <!-- generate id for ./OT/C/AwUConst (e.g. WhenFilteringCondition/AND/Equal/AwUConst) as sequence number -->
                                <xsl:with-param name="SN_AwUConst"
                                    select="replace(generate-id(*/AwUConst), '([a-z]|[A-Z])', '')"/>
                            </xsl:call-template>
                        </xsl:when>
                        <!-- case3.2 without Operater inside AwUExpression
                            e.g.
                            <When-Filtering-Condition>
                                <AND>
                                    <Equal>
                                        <MonitoredData Name=""/>
                                        <AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
                                    </Equal>
                                    <NotEqual>
                                        <MonitoredData Name=""/>
                                        <AwUConst Value="" Unit=""/> (or <AwUConst Value=""/>
                                    </NotEqual>
                                </AND>
                            </When-Filtering-Condition>
                        -->
                        <xsl:otherwise>
                            <xsl:variable name="AwUExpValue" select="./AwUConst/@Value"/>
                            <xsl:variable name="AwUExpUnit" select="./AwUConst/@Unit"/>
                            <xsl:call-template name="Comparision">
                                <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                                <xsl:with-param name="MDIN" select="./MonitoredData/@Name"/>
                                <xsl:with-param name="C" select="local-name()"/>
                                <xsl:with-param name="AwUExpValue" select="$AwUExpValue"/>
                                <xsl:with-param name="AwUExpUnit" select="$AwUExpUnit"/>
                                <xsl:with-param name="OURI"
                                    select="substring-before($AwUExpUnit, ':')"/>
                                <xsl:with-param name="T" select="substring-after($AwUExpUnit, ':')"/>
                                <!-- generate id for ./OT/C/MonitoredData (e.g. WhenFilteringCondition/AND/Equal/MonitoredData) as sequence number -->
                                <xsl:with-param name="SN_MonitoredData"
                                    select="replace(generate-id(./MonitoredData), '([a-z]|[A-Z])', '')"/>
                                <!-- generate id for ./OT/C/AwUConst (e.g. WhenFilteringCondition/AND/Equal/AwUConst) as sequence number -->
                                <xsl:with-param name="SN_AwUConst"
                                    select="replace(generate-id(./AwUConst), '([a-z]|[A-Z])', '')"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:element>
        </xsl:if>
    </xsl:template>

    <!-- Template for comparision -->
    <xsl:template name="Comparision">
        <xsl:param name="MD_MASCID"/>
        <xsl:param name="MDIN"/>
        <xsl:param name="C"/>
        <xsl:param name="AwUExpValue"/>
        <xsl:param name="AwUExpUnit"/>
        <xsl:param name="OURI"/>
        <xsl:param name="T"/>
        <xsl:param name="SN_MonitoredData"/>
        <xsl:param name="SN_AwUConst"/>
        <xsl:param name="operater"/>

        <xsl:element name="masc-ex:BooleanExpression">
            <xsl:attribute name="MASCID">
                <xsl:text>MD-BE-</xsl:text>
                <xsl:value-of select="$MD_MASCID"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$MDIN"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$SN_MonitoredData"/>
            </xsl:attribute>
            <xsl:element name="masc-ex:ArithmeticWithUnitComparator">
                <xsl:attribute name="Type">
                    <xsl:choose>
                        <xsl:when test="lower-case(string($C))='equal'">
                            <xsl:text>==</xsl:text>
                        </xsl:when>
                        <xsl:when test="lower-case(string($C))='notequal'">
                            <xsl:text>!=</xsl:text>
                        </xsl:when>
                        <xsl:when test="lower-case(string($C))='less'">
                            <xsl:text disable-output-escaping="yes">&lt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="lower-case(string($C))='greater'">
                            <xsl:text disable-output-escaping="yes">&gt;</xsl:text>
                        </xsl:when>
                        <xsl:when test="lower-case(string($C))='lessorequal'">
                            <xsl:text disable-output-escaping="yes">&lt;=</xsl:text>
                        </xsl:when>
                        <xsl:when test="lower-case(string($C))='greaterorequal'">
                            <xsl:text disable-output-escaping="yes">&gt;=</xsl:text>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="$C"/>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </xsl:element>
            <xsl:element name="masc-ex:ArithmeticWithUnitExpression">
                <xsl:attribute name="MASCID">
                    <xsl:text>MD-AE-</xsl:text>
                    <xsl:value-of select="$MD_MASCID"/>
                    <xsl:text>-</xsl:text>
                    <xsl:value-of select="$MDIN"/>
                    <xsl:text>-</xsl:text>
                    <xsl:value-of select="$SN_AwUConst"/>
                </xsl:attribute>
                <xsl:element name="masc-gp:MonitoredDataItemRef">
                    <xsl:attribute name="To">
                        <xsl:value-of select="$tns"/>
                        <xsl:value-of select="$MDIN"/>
                    </xsl:attribute>
                </xsl:element>
            </xsl:element>
            <xsl:choose>
                <!-- sub-case1. Arithmetic Expression with operater -->
                <xsl:when test="string($operater)='Add' or string($operater)='Multiply'">
                    <xsl:call-template name="AWU_with_Operator">
                        <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                        <xsl:with-param name="MDIN" select="$MDIN"/>
                        <xsl:with-param name="O" select="$operater"/>
                        <xsl:with-param name="AV" select="$AwUExpValue"/>
                        <xsl:with-param name="OURI" select="$OURI"/>
                        <xsl:with-param name="T" select="$T"/>
                        <xsl:with-param name="SN" select="$SN_AwUConst"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <!-- subCase2. Arithmetic Expression With Unit and without operater -->
                    <xsl:if test="$AwUExpUnit">
                        <xsl:call-template name="Arithmetic_With_Unit">
                            <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                            <xsl:with-param name="MDIN" select="$MDIN"/>
                            <xsl:with-param name="SN" select="$SN_AwUConst"/>
                            <xsl:with-param name="AV" select="$AwUExpValue"/>
                            <xsl:with-param name="OURI" select="$OURI"/>
                            <xsl:with-param name="T" select="$T"/>
                        </xsl:call-template>
                    </xsl:if>
                    <!-- subCase3. if ArithmeticExpression has no Unit and without operater -->
                    <xsl:if test="not($AwUExpUnit)">
                        <xsl:call-template name="Arithmetic_Constants_Without_Unit">
                            <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                            <xsl:with-param name="MDIN" select="$MDIN"/>
                            <xsl:with-param name="SN" select="$SN_AwUConst"/>
                            <xsl:with-param name="CV" select="$AwUExpValue"/>
                        </xsl:call-template>
                    </xsl:if>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:element>
    </xsl:template>

    <!-- template for Arithmetic-with-unit constants -->
    <xsl:template name="Arithmetic_With_Unit">
        <xsl:param name="MD_MASCID"/>
        <xsl:param name="MDIN"/>
        <xsl:param name="SN"/>
        <xsl:param name="AV"/>
        <xsl:param name="OURI"/>
        <xsl:param name="T"/>
        <xsl:element name="masc-ex:ArithmeticWithUnitExpression">
            <xsl:attribute name="MASCID">
                <xsl:text>MD-AUE-</xsl:text>
                <xsl:value-of select="$MD_MASCID"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$MDIN"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$SN"/>
            </xsl:attribute>
            <xsl:element name="masc-ex:ArithmeticWithUnitConstant">
                <xsl:element name="masc-ex:ArithmeticValue">
                    <xsl:attribute name="Value" select="$AV"/>
                </xsl:element>
                <xsl:element name="masc-ex:Unit">
                    <xsl:attribute name="Ontology" select="$OURI"/>
                    <xsl:attribute name="Type" select="$T"/>
                </xsl:element>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <!-- template for arithmetic-with-unit -->
    <xsl:template name="Arithmetic_Constants_Without_Unit">
        <xsl:param name="MD_MASCID"/>
        <xsl:param name="MDIN"/>
        <xsl:param name="SN"/>
        <xsl:param name="CV"/>
        <xsl:element name="masc-ex:ArithmeticExpression">
            <xsl:attribute name="MASCID">
                <xsl:text>MD-AC-</xsl:text>
                <xsl:value-of select="$MD_MASCID"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$MDIN"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$SN"/>
            </xsl:attribute>
            <xsl:element name="masc-ex:FloatConstant">
                <xsl:attribute name="Value" select="$CV"/>
            </xsl:element>
        </xsl:element>
    </xsl:template>

    <!-- template for arithmetic-with-unit operators -->
    <xsl:template name="AWU_with_Operator">
        <xsl:param name="MD_MASCID"/>
        <xsl:param name="MDIN"/>
        <xsl:param name="SN"/>
        <xsl:param name="O"/>
        <xsl:param name="AV"/>
        <xsl:param name="OURI"/>
        <xsl:param name="T"/>
        <xsl:element name="masc-ex:ArithmeticWithUnitExpression">
            <xsl:attribute name="MASCID">
                <xsl:text>MD-AUE-</xsl:text>
                <xsl:value-of select="$MD_MASCID"/>
                <xsl:text>-</xsl:text>
                <xsl:value-of select="$SN"/>
            </xsl:attribute>
            <xsl:element name="masc-ex:BinaryArithmeticWithUnitOperator">
                <xsl:attribute name="Type">
                    <xsl:choose>
                        <xsl:when test="lower-case(string($O))='add'">
                            <xsl:text>+</xsl:text>
                        </xsl:when>
                        <xsl:when test="lower-case(string($O))='multiply'">
                            <xsl:text>*</xsl:text>
                        </xsl:when>
                    </xsl:choose>
                </xsl:attribute>
            </xsl:element>
            <xsl:element name="masc-ex:ArithmeticWithUnitExpression">
                <xsl:attribute name="MASCID">
                    <xsl:text>MD-AUE-</xsl:text>
                    <xsl:value-of select="$MD_MASCID"/>
                    <xsl:text>-</xsl:text>
                    <xsl:value-of select="$MDIN"/>
                    <xsl:text>-</xsl:text>
                    <xsl:value-of select="$SN"/>
                </xsl:attribute>
                <xsl:element name="masc-gp:MonitoredDataItemRef">
                    <xsl:attribute name="To">
                        <xsl:value-of select="$tns"/>
                        <xsl:value-of select="$MDIN"/>
                    </xsl:attribute>
                </xsl:element>
            </xsl:element>
            <xsl:choose>
                <!-- sub-case1. for Add Operater, the Arithmetic Expression must has Unit -->
                <xsl:when test="string($O)='Add'">
                    <xsl:call-template name="Arithmetic_With_Unit">
                        <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                        <xsl:with-param name="MDIN" select="$MDIN"/>
                        <xsl:with-param name="SN" select="$SN"/>
                        <xsl:with-param name="AV" select="$AV"/>
                        <xsl:with-param name="OURI" select="$OURI"/>
                        <xsl:with-param name="T" select="$T"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- sub-case2.for Multiply Operater, the Arithmetic Expression must has NO Unit -->
                <xsl:when test="string($O)='Multiply'">
                    <xsl:call-template name="Arithmetic_Constants_Without_Unit">
                        <xsl:with-param name="SN" select="$SN"/>
                        <xsl:with-param name="MDIN" select="$MDIN"/>
                        <xsl:with-param name="MD_MASCID" select="$MD_MASCID"/>
                        <xsl:with-param name="CV" select="$AV"/>
                    </xsl:call-template>
                </xsl:when>
            </xsl:choose>
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>
